<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Marketing_wx extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 */
	public function index()
	{
		
		$this->load->model('Schoolo2o_model');
        $wx_mp = $this->Schoolo2o_model->get_wx_mp($this->user_data['school_id']);
        // print_r($wx_mp);exit;
        if($wx_mp){
        	$this->smarty->assign("wx_mp", $wx_mp);
        }

		$this->smarty->assign("menu", "marketing");
		$this->smarty->display("marketing/marketing_wx.html");
	}

	public function setting($value='')
	{
		$this->load->model('Schoolo2o_model');
		$post_data = $this->input->post();
		if($post_data){
			$this->Schoolo2o_model->update_wx_app_secret($this->user_data['school_id'],$post_data['app_secret']);
			header("Location:/marketing_wx/setting");exit;
		}
		$wx_mp = $this->Schoolo2o_model->get_wx_mp($this->user_data['school_id']);
        // print_r($wx_mp);exit;
        if($wx_mp){
        	$this->smarty->assign("app_secret", $wx_mp['app_secret']);

        }

		$this->smarty->assign("menu", "marketing");
		$this->smarty->display("marketing/marketing_wx_setting.html");
	}

	public function menu($value='')
	{
		$this->smarty->assign("menu", "marketing");
		$this->smarty->display("marketing/marketing_wx_menu.html");
	}

	/***
     * 绑定微信公众号
     */
    function wx_bind(){

        //获取syswxconfig
        $this->load->model('Syswxconfig_model');
        $wxconfig = $this->Syswxconfig_model->get_wxconfig();


        //1   获取第三方平台component_access_token //TODO:需要优化，access_token要保存
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, 'https://api.weixin.qq.com/cgi-bin/component/api_component_token');
        curl_setopt($curl, CURLOPT_POST, 1);
        $post_data = array(
            'component_appid' => $wxconfig['appid'],
            'component_appsecret' => $wxconfig['appsecret'],
            'component_verify_ticket' => $wxconfig['component_verify_ticket']
        );

        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_data));
        ob_start();
        curl_exec($curl);
        // $error    = curl_error($curl);
        // $info    = curl_getinfo($curl);
        // echo "{'status':1,'code':'','msg':'".$info."'}";
        //
        $result = ob_get_contents();
        // print_r($result);exit;

        ob_end_clean();
        curl_close($curl);
        $component_access_token = json_decode($result)->component_access_token;


        $this->Syswxconfig_model->update_token($component_access_token);

        //2   获取预授权码pre_auth_code
        $curl1 = curl_init();
        curl_setopt($curl1, CURLOPT_URL, 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token='.$component_access_token);
        curl_setopt($curl1, CURLOPT_POST, 1);
        $post_data1 = array(
            'component_appid' => $wxconfig['appid']
        );

        curl_setopt($curl1, CURLOPT_POSTFIELDS, json_encode($post_data1));
        ob_start();
        curl_exec($curl1);
        // $error    = curl_error($curl);
        // $info    = curl_getinfo($curl);
        // echo "{'status':1,'code':'','msg':'".$info."'}";
        //
        $result1 = ob_get_contents();
        // print_r($result1);exit;
        ob_end_clean();
        curl_close($curl1);

        $pre_auth_code = json_decode($result1)->pre_auth_code;
//        print_r($pre_auth_code);
//        print_r(json_decode($result1)->pre_auth_code);

        if(empty($pre_auth_code)==false)
        {
            $r_url = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid='.$wxconfig['appid'].'&pre_auth_code='.$pre_auth_code.'&redirect_uri='.urlencode("{$wxconfig['callback_url']}");
//            print_r($r_url);
            header("Location:".$r_url);
            exit;
        }





        print_r("ok");
    }

    /**
     * 微信公众号授权回调
     */
    function wx_bind_back(){
        $school_id = $this->user_data['school_id'];
        //http://center.weyowo.com/marketing/wx_back?auth_code=queryauthcode@@@N7DFV-pPI3BZJmGijtoYpTqaJlL_aPvFRp70lJ6YiX5m44Z2Qui7n-DPPmA53bV5urIlk-2pvPB8OuJi12DvcA&expires_in=3600
        $auth_code = $this->input->get('auth_code');

        $this->load->model('Syswxconfig_model');
        $wxconfig = $this->Syswxconfig_model->get_wxconfig();

        //1   使用授权码换取公众号的接口调用凭据和授权信息
        $curl2 = curl_init();
        curl_setopt($curl2, CURLOPT_URL, 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token='.$wxconfig['component_access_token']);
        curl_setopt($curl2, CURLOPT_POST, 1);
        $post_data2 = array(
            'component_appid' => $wxconfig['appid'],
            'authorization_code' => $auth_code
        );

        curl_setopt($curl2, CURLOPT_POSTFIELDS, json_encode($post_data2));
        ob_start();
        curl_exec($curl2);
        $result2 = ob_get_contents();

        ob_end_clean();
        curl_close($curl2);



        //保存会回调信息到店铺
        $app_id = json_decode($result2)->authorization_info->authorizer_appid;
        $access_token = json_decode($result2)->authorization_info->authorizer_access_token;
        $refresh_token = json_decode($result2)->authorization_info->authorizer_refresh_token;
        $expires_in = json_decode($result2)->authorization_info->expires_in;
        $authorized_func_info = json_encode(json_decode($result2)->authorization_info->func_info);

        //保存到数据库
        $this->load->model('Schoolo2o_model');
        $this->Schoolo2o_model->bind_wx_mp($school_id,$app_id,$access_token,$refresh_token,$expires_in,$authorized_func_info);


        //2   获取授权方的公众号帐号基本信息
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token='.$wxconfig['component_access_token']);
        curl_setopt($curl, CURLOPT_POST, 1);
        $post_data = array(
            'component_appid' => $wxconfig['appid'],
            'authorizer_appid' => $app_id
        );

        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_data));
        ob_start();
        curl_exec($curl);
        $result = ob_get_contents();

        ob_end_clean();
        curl_close($curl);
        
        //保存绑定的公众号信息
        $app_name = json_decode($result)->authorizer_info->nick_name;
        $app_head_img = json_decode($result)->authorizer_info->head_img;
        $app_user_name = json_decode($result)->authorizer_info->user_name ;
        $app_type  = json_decode($result)->authorizer_info->service_type_info->id;
        $app_verify_type = json_decode($result)->authorizer_info->verify_type_info->id;
        $app_principal_name = json_decode($result)->authorizer_info->principal_name;
        $app_alias = json_decode($result)->authorizer_info->alias;
        $app_qrcode_url = json_decode($result)->authorizer_info->qrcode_url;

        $this->Schoolo2o_model->update_wxopen_info($school_id,$app_name,$app_head_img,$app_user_name,$app_type,$app_verify_type,$app_principal_name,$app_alias,$app_qrcode_url);
       
        header("Location:/marketing_wx");
        exit;
    }


	
}
